<?php

require_once "../../maincore.php";
require_once THEMES."templates/header.php";

if (file_exists(INFUSIONS."buddys_system/locale/".$settings['locale'].".php")) {
	include INFUSIONS."buddys_system/locale/".$settings['locale'].".php";
} else {
	include INFUSIONS."buddys_system/locale/English.php";
}



$buddy = (!isset($_GET['buddy']) ? '' : $_GET['buddy']);

if (!isset($_REQUEST['buddy_to'])) $_REQUEST['buddy_to']='';
$buddy_to = $_REQUEST['buddy_to'];

if (!isset($_REQUEST['buddy_from'])) $_REQUEST['buddy_from']='';
$buddy_from = $_REQUEST['buddy_from'];


opentable($locale['mf01']);

if (iMEMBER) {

echo "<table align='center' cellpadding='2' cellspacing='4' border='0' width='100%'>
		<tr>
			<td>".$locale['mf02']."</td>
		</tr>
</table>
<br>
<table border='0' cellpadding='0' cellspacing='1' width='100%' class='tbl-border'>
  <tr>
    <td class='tbl2' style='white-space:nowrap'><b>".$locale['mf05']."</b></td>
    <td class='tbl1' style='white-space:nowrap'><a href='".BASEDIR."membersab.php' class='side'>".$locale['mf03']."</a></td>
    
  </tr>
</table><hr />";
echo "<center><b>".$locale['mf08']."</b></center><hr />";

echo "<table border='0' cellpadding='0' cellspacing='1' width='100%' class='tbl-border'>
		<tr>
			<td class='tbl2' style='white-space:nowrap'>".$locale['mf09']."</td>
                        <td class='tbl2' style='white-space:nowrap'>".$locale['mf10']."</td>
			<td class='tbl2' style='white-space:nowrap'>".$locale['mf11']."</td>
			<td class='tbl2' style='white-space:nowrap'>".$locale['mf12']."</td>
		</tr>";



$result = dbquery("SELECT * FROM ".DB_PREFIX."buddys
WHERE buddy_from = ".$userdata['user_id']." OR buddy_to = ".$userdata['user_id']."");

while($data = dbarray($result)){

$result_to_from = dbquery("SELECT * FROM ".DB_PREFIX."buddys
WHERE buddy_to = ".$userdata['user_id']." AND buddy_from = ".$data['buddy_to']."");
$data_to_from = dbarray($result_to_from);


if ($data['buddy_to'] != $userdata['user_id'] AND $data_to_from['buddy_to'] == true) {

$result_user = dbquery("SELECT * FROM ".DB_PREFIX."users WHERE user_id = ".$data['buddy_to']."");
$data_user = dbarray($result_user);



$check = "1";




echo "	<tr>";

if ($data_user['user_avatar'] && file_exists(IMAGES."avatars/".$data_user['user_avatar'])) {

		$avatar = "<img style='margin:2px' height='50' width='50' border='0' src='".IMAGES."avatars/".$data_user['user_avatar']."' alt='' />";

	} else {

		$avatar = "<img style='margin:2px' height='50' width='50' border='0' src='".IMAGES."avatars/noav.gif' alt='' />";

		}


			echo "<td width='1%' class='tbl1'>".$avatar."</td>
                        <td width='20%' class='tbl1'><b><a href='".BASEDIR."profile.php?lookup=".$data_user['user_id']."'>".$data_user['user_name']."</a></b></td>
			<td width='27%' class='tbl1'>".$locale['mf24']."</td>
			<td class='tbl1' width='43%'><a href='buddys.php?buddy=b&buddy_from=".$userdata['user_id']."&buddy_to=".$data['buddy_to']."'>".$locale['mf25']."</a></td>
		</tr>";

if ($buddy == 'b') {
mysql_query("DELETE FROM ".DB_PREFIX."buddys
WHERE buddy_from = $buddy_from AND buddy_to = $buddy_to");
mysql_query("UPDATE ".DB_PREFIX."buddys SET buddy_status=2
WHERE buddy_from = $buddy_to AND buddy_to = $buddy_from");

redirect("buddys.php");
}

}}

$result = dbquery("SELECT * FROM ".DB_PREFIX."buddys
WHERE buddy_to = ".$userdata['user_id']."");

while($data = dbarray($result)){

$result_from = dbquery("SELECT * FROM ".DB_PREFIX."buddys
WHERE buddy_id = ".$data['buddy_id']."");

while($data_from = dbarray($result_from)){

$result_from_check = dbquery("SELECT * FROM ".DB_PREFIX."buddys
WHERE buddy_to = ".$data_from['buddy_from']." AND buddy_from = ".$data_from['buddy_to']."");
$data_from_check = dbarray($result_from_check);

if ($data_from_check != true) {

$result_user = dbquery("SELECT * FROM ".DB_PREFIX."users WHERE user_id = ".$data_from['buddy_from']."");
$data_user = dbarray($result_user);

if ($data_from['buddy_status'] == '1') {

$check = "1";

echo "	<tr>";

if ($data_user['user_avatar'] && file_exists(IMAGES."avatars/".$data_user['user_avatar'])) {

		$avatar = "<img style='margin:2px' height='50' width='50' border='0' src='".IMAGES."avatars/".$data_user['user_avatar']."' alt='' />";

	} else {

		$avatar = "<img style='margin:2px' height='50' width='50' border='0' src='".IMAGES."avatars/noav.gif' alt='' />";

		}


			echo "
                        <td width='1%' class='tbl1'>".$avatar."</td>
			<td width='20%' class='tbl1'><b><a href='".BASEDIR."profile.php?lookup=".$data_user['user_id']."'>".$data_user['user_name']."</a></b></td>
			<td width='27%' class='tbl1'>".$locale['mf20']."</td><td class='tbl1' width='43%'><a href='buddys.php?buddy=a&buddy_from=".$userdata['user_id']."&buddy_to=".$data_from['buddy_from']."'>".$locale['mf31']."</a> | <a href='buddys.php?buddy=u&buddy_from=".$userdata['user_id']."&buddy_to=".$data_from['buddy_from']."'>".$locale['mf32']."</a></td>
		</tr>";
}elseif($data_from['buddy_status'] == '2') {

if ($buddy == 'g') {
mysql_query("DELETE FROM ".DB_PREFIX."buddys
WHERE buddy_from = $buddy_to AND buddy_to = $buddy_from");
redirect("buddys.php");
}

}else{

}

if ($buddy == 'a') {
$datum = time();
dbquery("INSERT INTO ".DB_PREFIX."buddys (buddy_id, buddy_from, buddy_to, buddy_date, buddy_status) VALUES ('','$buddy_from', '$buddy_to', '$datum', '1')");
redirect("buddys.php");
}

if ($buddy == 'u') {
mysql_query("UPDATE ".DB_PREFIX."buddys SET buddy_status=0 WHERE buddy_from = $buddy_to AND buddy_to = $buddy_from");
redirect("buddys.php");
}


}}}

$result = dbquery("SELECT * FROM ".DB_PREFIX."buddys
WHERE buddy_from = ".$userdata['user_id']."");

while($data = dbarray($result)){

$result_to = dbquery("SELECT * FROM ".DB_PREFIX."buddys
WHERE buddy_id = ".$data['buddy_id']."");

while($data_to = dbarray($result_to)){

$result_to_check = dbquery("SELECT * FROM ".DB_PREFIX."buddys
WHERE buddy_to = ".$data_to['buddy_from']." AND buddy_from = ".$data_to['buddy_to']."");
$data_to_check = dbarray($result_to_check);

if ($data_to_check != true) {

$result_user = dbquery("SELECT * FROM ".DB_PREFIX."users WHERE user_id = ".$data_to['buddy_to']."");
$data_user = dbarray($result_user);

if ($data_to['buddy_status'] == '1') {

$check = "1";

echo "	<tr>";

if ($data_user['user_avatar'] && file_exists(IMAGES."avatars/".$data_user['user_avatar'])) {

		$avatar = "<img style='margin:2px' height='50' width='50' border='0' src='".IMAGES."avatars/".$data_user['user_avatar']."' alt='' />";

	} else {

		$avatar = "<img style='margin:2px' height='50' width='50' border='0' src='".IMAGES."avatars/noav.gif' alt='' />";

		}


			echo "
                        <td width='1%' class='tbl1'>".$avatar."</td>
			<td width='20%' class='tbl1'><b><a href='".BASEDIR."profile.php?lookup=".$data_user['user_id']."'>".$data_user['user_name']."</a></b></td>
			<td width='27%' class='tbl1'>".$locale['mf21']."</td><td width='43%' class='tbl1'><a href='buddys.php?buddy=z&buddy_from=".$userdata['user_id']."&buddy_to=".$data_to['buddy_to']."'>".$locale['mf30']."</a></td></tr>";

if ($buddy == 'z') {
mysql_query("DELETE FROM ".DB_PREFIX."buddys
WHERE buddy_from = $buddy_from AND buddy_to = $buddy_to");
redirect("buddys.php");
}

}elseif($data_to['buddy_status'] == '2') {

$check = "1";

echo "<tr>";

if ($data_user['user_avatar'] && file_exists(IMAGES."avatars/".$data_user['user_avatar'])) {

		$avatar = "<img style='margin:2px' height='50' width='50' border='0' src='".IMAGES."avatars/".$data_user['user_avatar']."' alt='' />";

	} else {

		$avatar = "<img style='margin:2px' height='50' width='50' border='0' src='".IMAGES."avatars/noav.gif' alt='' />";

		}


echo "<td width='1%' class='tbl1'>".$avatar."</td>
     <td width='20%' class='tbl1'><b><a href='".BASEDIR."profile.php?lookup=".$data_user['user_id']."'>".$data_user['user_name']."</a></b></td>
      <td width='27%' class='tbl1'>".$locale['mf23']."</td>
<td class='tbl1' width='43%'><a href='buddys.php?buddy=l&buddy_from=".$userdata['user_id']."&buddy_to=".$data_to['buddy_to']."'>".$locale['mf33']."</a></td></tr>";

if ($buddy == 'l') {
mysql_query("DELETE FROM ".DB_PREFIX."buddys
WHERE buddy_from = $buddy_from AND buddy_to = $buddy_to");
redirect("buddys.php");
}

}else{

$check = "1";

echo "	<tr>";

if ($data_user['user_avatar'] && file_exists(IMAGES."avatars/".$data_user['user_avatar'])) {

		$avatar = "<img style='margin:2px' height='50' width='50' border='0' src='".IMAGES."avatars/".$data_user['user_avatar']."' alt='' />";

	} else {

		$avatar = "<img style='margin:2px' height='50' width='50' border='0' src='".IMAGES."avatars/noav.gif' alt='' />";

		}


			echo "
                        <td width='1%' class='tbl1'>".$avatar."</td>
			<td width='20%' class='tbl1'><b>".$data_user['user_name']."</b></td>
			<td width='27%' class='tbl1'>".$locale['mf22']."</td><td width='43%' class='tbl1'><a href='buddys.php?buddy=l&buddy_from=".$userdata['user_id']."&buddy_to=".$data_to['buddy_to']."'>".$locale['mf33']."</a></td></tr>";

if ($buddy == 'l') {
mysql_query("DELETE FROM ".DB_PREFIX."buddys
WHERE buddy_from = $buddy_from AND buddy_to = $buddy_to");
redirect("buddys.php");
}

}


}}}

if (!isset($check)) $check='';

echo "</table>";


$result = mysql_query("SELECT * FROM ".DB_PREFIX."buddys WHERE buddy_from = ".$userdata['user_id']." OR buddy_to = ".$userdata['user_id']."");
$anzahl = mysql_num_rows($result);


if ($check <> "1") {

echo "<center><br>".$locale['mf40']."<br><br></center>";

}

}



closetable();
require_once THEMES."templates/footer.php";


?>
